<?php
	include('inc/head.php');

if(isset($_POST['email'])){
	$em = $_POST['email'];
	$query = mysql_query("SELECT * FROM users WHERE email='{$em}' and `delete flag` = 'N'");
	$err = 1;
	$msg = "The email provided is incorrect or does not exist.";
	while($row = mysql_fetch_array($query)) {
		$password = genRandomString(10);
		$hashedPwd = sha1($password);
		$time = time('now');
		mysql_query("update users set `delete flag` = 'Y' WHERE email='{$em}' and `delete flag` = 'N'");
		mysql_query("INSERT INTO `users` (`userid`,`username`, `pwd`, `email`, `profile image path`, `time`, `delete flag`,`isadmin`,`email varification`) 
					VALUES ('{$row['userid']}','{$row['username']}', '{$hashedPwd}', '{$row['email']}', '{$row['profile image path']}', '{$time}','N', 'N','Y')");
		
		
		$subject = 'Next Twist: Your Password has been reset';
		
		// message
		$message = '
						<html>
						<head>
						  <title>New password for Nexttwist account!!</title>
						</head>
						<body>
						  <table>
							<tr>
							  Hi,<br/>
							  Your password has been changed successfully. Your new password is: '.trim($password).'<br/>
							  Nexttwist Team
							</tr>
						  </table>
						</body>
						</html>
						';
		
		// To send HTML mail, the Content-type header must be set
        $headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		$headers .= "From: NextTwist Team <noreply@nexttwist.com>" . "\r\n";
			
		// Mail it
		if(mail($em, $subject, $message, $headers)) {
			$msg = "Your password has been reset. Please check your email <strong>".$em."</strong>  for the new password.";
		} else
			$msg= "Unable to send a new password. Please try again later.";
		;
		//	$msg = "your password is reset please check your mail for new password.";
	}
}
?>
<script type="text/javascript">
function checkforgot(){
	var check = 0;
	var email = $('#forgotEmail').val();
	if (email == '' || email == 'null' || email == 'Email'){
		$("#forgotError").show('blind', { direction: 'vertical' }, 300);
		$("#fogotNull").show();
		check = 1;
	}
	if(check == 1) { return false;}
		else { return true; }
}
</script>
<div class="topShadow"></div>
<div id="content" class="forgotWrap">
    <div class="reset-frm">
        <h1>Reset Password?</h1>
    <?php if($err == 1){ ?>
        <div class="errorMsg clear" style="margin: 15px 0; padding: 5px; "><span><?php echo $msg; ?></span></div>
    <?php } else {?>
        <p style="margin: 15px 0;">We will send you a new password in your email.</p>
     <?php } ?>
        <form id="usrForgot" action="#" method="post" onSubmit="javascript: return checkforgot();">
            <div class="reg-frm-item item">
                <div class="formNameWrap"><div id="showFormEmail" class="formName"><label>Email</label></div></div>
                <img id="forgotLoading"  class="hide left" src="img/loading.gif" />
                <input id="forgotEmail" name="email" class="regInputItem left ghost_text" placeholder="Email" type="text">
                <button class="forgotBtn btnB left" name="forgotBtn" type="submit" style="margin-left: 15px;">Reset Password</button>
                <div class="clear"></div>
                <div id="forgotError" class="errorMsg hide">
                    <span id="fogotNull" class="hide">You have left your email blank!</span>
                </div>
                </div>
        </form>
    </div>    
    <div class="clear"></div>
</div>
<div class="bottomShadow"></div>